from handlers.base_handler import BaseHandler
from blls.file_upload_blls.file_upload_bll import FileUploadBLL
from utils.form_data_params_util import FormDataParamsUtil
from utils.handler_util import HandlerUtil


class HealthHandler(BaseHandler):
    async def get(self):
        res = FileUploadBLL(request=self).health()
        if res.code != 200:
            self.set_status(500, '')
        return self.write(HandlerUtil.set_response(data=res.data, message=res.message,
                                                   is_success=res.is_success, code=res.code))


class FileUploadHandler(BaseHandler):
    async def post(self):
        files = self.request.files
        file_size = self.request.headers.get('Content-Length')
        required_params = [
            ["app_id", True, ""],
            ["timestamp", True, ""],
            ["scene", True, ""],
            ["sign", True, ""]
        ]
        data_res = FormDataParamsUtil(self.request.arguments).check(required_params)
        if not data_res.is_success:
            return self.write(HandlerUtil.set_failure_response(message=data_res.message))
        data = data_res.data['res']
        res = FileUploadBLL(request=self).file_upload(files, file_size, **data)
        return self.write(HandlerUtil.set_response(data=res.data, message=res.message,
                                                   is_success=res.is_success, code=res.code))